﻿using System;

namespace Tetris
{
    public class KeyboardInterface : IUserInterface
    {
        public void ProcessInput()
        {
            while (Console.KeyAvailable)
            {
                var keyInfo = Console.ReadKey(true);
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        // rotate piece
                        if (this.OnUpPressed != null)
                        {
                            this.OnUpPressed(this, new EventArgs());
                        }
                        break;
                    case ConsoleKey.DownArrow:
                        // drop piece
                        if (this.OnDownPressed != null)
                        {
                            this.OnDownPressed(this, new EventArgs());
                        }
                        break;
                    case ConsoleKey.LeftArrow:
                        // move piece left
                        if (this.OnLeftPressed != null)
                        {
                            this.OnLeftPressed(this, new EventArgs());
                        }
                        break;
                    case ConsoleKey.RightArrow:
                        // move piece right
                        if (this.OnRightPressed != null)
                        {
                            this.OnRightPressed(this, new EventArgs());
                        }
                        break;
                }
            }
        }

        public event EventHandler OnUpPressed;

        public event EventHandler OnDownPressed;

        public event EventHandler OnLeftPressed;

        public event EventHandler OnRightPressed;

        public event EventHandler OnActionPressed;
    }
}
